<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      width: 1200px;
      margin: 0 auto;
    }

    .search {
      width: 100%;
      border: 1px solid blue;
      padding: 10px;
    }

    .content {
      width: 100%;
      text-align: center;
    }

    table {
      width: 1200px;
    }

    tr {
      height: 30px;
    }

    table,
    tr,
    th,
    td {
      border: 1px solid black;
      border-collapse: collapse;
    }
  </style>
</head>

<body>
  <div class="box">
    <div class="search">姓名：<input type="text"><button>搜索</button><a href="add.html">添加</a></div>
    <div class="content">
      <table>
        <thead>
          <tr>
            <th>序号</th>
            <th>学号</th>
            <th>姓名</th>
            <th>班级</th>
            <th>性别</th>
            <th>年龄</th>
            <th>电话</th>
            <th>爱好</th>
            <th>地址</th>
            <th>备注</th>
            <th>日期</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody></tbody>
      </table>
    </div>
  </div>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script>
    axios.defaults.baseURL = 'http://localhost:3008/api/student/'
    function getData(uname) {
      axios({
        url: 'getstudent',
        method: 'GET',
        params: {
          'name': uname
        }
      }).then(result => {
        console.log(result);
        let arr = result.data
        let str = ``
        arr.forEach((ele, index) => {
          str += `
              <tr>
                <td>${index + 1}</td>
                <td>${ele.id}</td>
                <td>${ele.name}</td>
                <td>${ele.clazz}</td>
                <td>${ele.gender}</td>
                <td>${ele.age}</td>
                <td>${ele.tel}</td>
                <td>${ele.hobby}</td>
                <td>${ele.address}</td>
                <td>${ele.remark}</td>
                <td>${ele.date}</td>
                <td>
                <a href="edit.html#${ele.id}">编辑</a>
                <a href="javascript:del(${ele.id})">删除</a>
                </td>
              </tr>
              `
        })
        document.querySelector('tbody').innerHTML = str
      }).catch(error => {
        console.log('出错');
      }).finally(() => {
        console.log('finally');
      })
    }
    getData()
    var btn = document.querySelector('button')
    btn.addEventListener('click', function (e) {
      var data = document.querySelector('input').value
      getData(data)
    })
    function del(id) {
      axios({
        url: 'removestudent',
        method: 'POST',
        data: { 'id': id }
      }).then(result => {
        console.log(result);
        let arr = result.data
        let str = ``
        arr.forEach((ele, index) => {
          str += `
              <tr>
                <td>${index + 1}</td>
                <td>${ele.id}</td>
                <td>${ele.name}</td>
                <td>${ele.clazz}</td>
                <td>${ele.gender}</td>
                <td>${ele.age}</td>
                <td>${ele.tel}</td>
                <td>${ele.hobby}</td>
                <td>${ele.address}</td>
                <td>${ele.remark}</td>
                <td>${ele.date}</td>
                <td>
                <a href="edit.html#${ele.id}">编辑</a>
                <a href="javascript:del(${ele.id})">删除</a>
                </td>
              </tr>
              `
        })
        document.querySelector('tbody').innerHTML = str
      }).catch(error => {
        console.log('出错');
      }).finally(() => {
        console.log('finally');
      })
    }
  </script>
</body>

</html>